//
//  OneHttpPingAction.m
//  iOSLaboratory
//
//  Created by yangshansi on 2017/4/25.
//  Copyright © 2017年 WiLL. All rights reserved.
//

#import "OneHttpPingAction.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>



@implementation OneHttpPingAction

/******************************************************************************/
/**** Customized Method                                                    ****/
/******************************************************************************/
#pragma mark - Customized Method

- (BOOL)doOnePing
{
    NSString *strURL = [self formatURLStringFromHost:self.hostName];
    
    NSURL *url = [NSURL URLWithString:strURL];
    if (url == nil) {
        return NO;
    }
    self.ip = [OneHttpPingAction queryIpWithDomain:url.host];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:url
                                    cachePolicy:(NSURLRequestReloadIgnoringCacheData)
                                    timeoutInterval:self.timeout];
    [request setHTTPMethod:@"POST"];
    
    if (self.data)
    {
        [request setHTTPBody:self.data];
    }
    
    NSURLResponse *response = nil;
    
    NSError *error = nil;
    
    self.sendTime = [[NSDate new] timeIntervalSince1970];
    
    NSData *result =
    [NSURLConnection sendSynchronousRequest:request
                          returningResponse:&response
                                      error:&error];
    
    if (!error)
    {
        self.recvTime = [[NSDate new] timeIntervalSince1970];
        self.interval = self.recvTime - self.sendTime;
        
        NSLog(@"#%ld received, size=%ld time=%.3f ms",
              self.sequenceNumber, result.length, self.interval*1000);
    }
    else
    {
        NSLog(@"#%ld error %ld %@",
              self.sequenceNumber, error.code, [error localizedDescription]);
        self.recvTime = 0;
        self.interval = 0;
    }

    
    
    return YES;
}


- (NSString *)formatURLStringFromHost:(NSString *)host
{
    NSMutableString *hostBuff = [NSMutableString new];
    
    if ([host hasPrefix:@"http://"] == NO &&
        [host hasPrefix:@"https://"] == NO)
    {
        [hostBuff appendString:@"http://"];
    }
    [hostBuff appendString:host];
    
    return [hostBuff description];
    
}




+ (NSString *)queryIpWithDomain:(NSString *)domain
{
    struct hostent *hs;
    struct sockaddr_in server;
    if ((hs = gethostbyname([domain UTF8String])) != NULL)
    {
        server.sin_addr = *((struct in_addr*)hs->h_addr_list[0]);
        return [NSString stringWithUTF8String:inet_ntoa(server.sin_addr)];
    }
    return nil;
}




/******************************************************************************/
/**** Lifecycle Method                                                     ****/
/******************************************************************************/
#pragma mark - Lifecycle Method

- (instancetype)initWithHost:(NSString *)hostName
              sequenceNumber:(NSInteger)sequenceNumber
                     timeout:(NSInteger)timeout
                        data:(NSData *)data
{
    self = [super initWithHost:hostName sequenceNumber:sequenceNumber
                       timeout:timeout data:data];
    if (self)
    {
        
    }
    return self;
}

@end
